package com.jmpt.fangbohui.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jmpt.fangbohui.bean.LoginBean;
import com.jmpt.fangbohui.db.LoginSQLiteOpenHelper;

/**
 * 
 *
 * @package com.jmpt.fangbohui.dao
 * @author cangJie
 * @description 用户登录信息dao
 *
 */
public class LoginSQLiteOperator {

	private Context context;

	public LoginSQLiteOperator(Context context) {
		super();
		this.context = context;
	}
	/**
	 * 
	 * @author cangJie
	 * @description 添加普通用户信息，先调用清空数据库方法
	 */
	public long addUser(LoginBean login){
		delete();
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("UserName", login.getRetval().getUserName());
		values.put("UserId", login.getRetval().getUserId());
		values.put("sessionid", login.getRetval().getSessionid());
		values.put("type", login.getRetval().getType());
		long insert = db.insert("login", null, values);
		db.close();
		return insert;
	}

	/**
	 * 
	 * @author cangJie
	 * @description 添加经纪人用户信息，先调用清空数据库方法
	 */
	public long addBroker(LoginBean login) {
		delete();
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("AgentName", login.getRetval().getAgentName());
		values.put("AgentId", login.getRetval().getAgentId());
		values.put("sessionid", login.getRetval().getSessionid());
		values.put("type", login.getRetval().getType());
		long insert = db.insert("login", null, values);
		db.close();
		return insert;
	}
	/**
	 * 
	 * @author cangJie
	 * @description 添加用户信息，先调用清空数据库方法
	 */
	public long add(LoginBean login) {
		delete();
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("UserName", login.getRetval().getUserName());
		values.put("UserId", login.getRetval().getUserId());
		values.put("AgentName", login.getRetval().getAgentName());
		values.put("AgentId", login.getRetval().getAgentId());
		values.put("type", login.getRetval().getType());
		long insert = db.insert("login", null, values);
		db.close();
		return insert;
	}

	/**
	 * 
	 * @author cangJie
	 * @description 清空用户信息
	 */
	public int delete() {
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getWritableDatabase();
		int delete = db.delete("login", null, null);
		db.close();
		return delete;
	}

//	public int update(LoginBean login) {
//		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
//		SQLiteDatabase db = helper.getWritableDatabase();
//		ContentValues values = new ContentValues();
//		values.put("userid", login.getUserid());
//		values.put("PriceName", login.getName());
//		values.put("type", login.getType());
//		values.put("phone", login.getPhone());
//		values.put("username", login.getUsername());
//		values.put("password", login.getPassword());
//		int update = db.update("login", values, " userid = ? ",
//				new String[] { login.getUserid() });
//		db.close();
//		return update;
//	}
	//type 1 user  2 broker
	/*
	 * "create table login (_id Integer primary key autoincrement , " +
				"UserName varchar(20), UserId varchar(20)," +
				"AgentName varchar(20),AgentId varchar(20) , type Integer)");
	 */
	/**
	 * 
	 * @author cangJie
	 * @description 查询储存的用户信息，若有数据，则返回查找到的bean对象，否则返回Null
	 */
	public LoginBean query() {
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor query = db.query("login", new String[] { "UserName", "UserId",
				"AgentName", "AgentId", "type","sessionId"}, null, null, null,
				null, null);
		LoginBean login = null;
		if (query.moveToNext()) {
			String UserName = query.getString(0);
			String UserId = query.getString(1);
			String AgentName = query.getString(2);
			String AgentId = query.getString(3);
			int type = query.getInt(4);
			String sessionId = query.getString(5);
			login = new LoginBean(UserName, UserId, AgentName, AgentId, type);
			login.getRetval().setSessionid(sessionId);
		}else{
			return null;
		}
		query.close();
		db.close();
		return login;
	}
	
	public List<String> queryUserSessionId(){
		List<String> list = new ArrayList<String> ();
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor query = db.query("login", new String[] {"UserId","AgentId","sessionid","type"}, null, null, null,
				null, null);
		if(query.moveToNext()){
			list.add(query.getString(0));
			list.add(query.getString(1));
			list.add(query.getString(2));
			list.add(query.getString(3));
		}
		query.close();
		db.close();
		return list;
	}
	
	public List<String> querySessionId(){
		List<String> list = new ArrayList<String> ();
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor query = db.query("login", new String[] {"AgentId","sessionid"}, null, null, null,
				null, null);
		if(query.moveToNext()){
			list.add(query.getString(0));
			list.add(query.getString(1));
		}
		query.close();
		db.close();
		return list;
	}
	public List<String> queryUserSessionId2(){
		List<String> list = new ArrayList<String> ();
		LoginSQLiteOpenHelper helper = new LoginSQLiteOpenHelper(context);
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor query = db.query("login", new String[] {"UserId","sessionid"}, null, null, null,
				null, null);
		if(query.moveToNext()){
			list.add(query.getString(0));
			list.add(query.getString(1));
		}
		query.close();
		db.close();
		return list;
	}
}
